package sample;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

import sample.util.CommonUtils;

import com.google.gdata.client.Query.CustomParameter;
import com.google.gdata.client.calendar.CalendarQuery;
import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.data.DateTime;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.calendar.CalendarEventEntry;
import com.google.gdata.data.calendar.CalendarEventFeed;
import com.google.gdata.data.extensions.When;
import com.google.gdata.data.extensions.Where;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

public class Calendar {

	public static void main(String[] args) {

		try {
			CalendarService calendarService = new CalendarService("CalendarSample");
			calendarService.setUserCredentials(args[0], args[1]);

			URL feedUrl = new URL("http://www.google.com/calendar/feeds/default/private/full");
			CalendarQuery calendarQuery = new CalendarQuery(feedUrl);

			GregorianCalendar today_start = new GregorianCalendar(TimeZone.getTimeZone("Asia/Tokyo"), Locale.JAPAN);
			today_start.set(GregorianCalendar.HOUR_OF_DAY, 0);
			today_start.set(GregorianCalendar.MINUTE, 0);
			today_start.set(GregorianCalendar.SECOND, 0);
			today_start.set(GregorianCalendar.MILLISECOND, 0);

			GregorianCalendar today_end = (GregorianCalendar) today_start.clone();
			today_end.add(GregorianCalendar.DATE, 1);
			today_end.add(GregorianCalendar.MILLISECOND, -1);

			calendarQuery.setMinimumStartTime(new DateTime(today_start.getTime()));
			calendarQuery.setMaximumStartTime(new DateTime(today_end.getTime()));

			calendarQuery.addCustomParameter(new CustomParameter("orderby", "starttime"));

			CalendarEventFeed resultFeed = calendarService.query(calendarQuery, CalendarEventFeed.class);

			CommonUtils.dump(resultFeed, System.out);
			System.out.println("\n");

			List<CalendarEventEntry> entries = resultFeed.getEntries();
			for (CalendarEventEntry entry : entries) {
				List<When> eventTimes = entry.getTimes();

				String eventTime = "";
				if (!eventTimes.isEmpty()) {
					When when = eventTimes.get(0);
					eventTime = when.getStartTime().toUiString() + "～" + when.getEndTime().toString();
				}

				List<Where> locations = entry.getLocations();
				String eventLocation = "";
				if (locations.isEmpty()) {
					Where where = locations.get(0);
					eventLocation = where.getLabel() + "," + where.getValueString() + "," + where.getRel();
				}

				System.out.println("\tタイトル=" + entry.getTitle().getPlainText() + "日時=" + eventTime);
				System.out.println("\t\t場所=" + eventLocation);

				entry.setTitle(new PlainTextConstruct("クライアント（PC）から書き換えましたよ！すごい！！！"));
				URL editUrl = new URL(entry.getEditLink().getHref());
				calendarService.update(editUrl, entry);
			}
			System.out.println("\n予定総数=" + entries.size());
		} catch (AuthenticationException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ServiceException e) {
			e.printStackTrace();
		}
	}
}
